{include file="common/header"/}
<div class="LM-container">
    <div class="LM-main">
        <fieldset class="layui-elem-field layui-field-title">
            <legend>友情链接{:lang('list')}</legend>
        </fieldset>
        <blockquote class="layui-elem-quote">
            <div class="LM-table">

                <div class="layui-inline">
                    <input type="text" id='keys' name="keys" lay-verify="required" placeholder="{:lang('pleaseEnter')}" autocomplete="off" class="layui-input">
                </div>
                <button class="layui-btn data-add-btn layui-btn-sm" lay-submit="" lay-filter="add" id="search">{:lang('search')}</button>
                <a href="{:url('add')}"  class="layui-btn layui-btn-sm layui-btn-warm" id="LM-add">{:lang('add')}友情链接</a>

            </div>
        </blockquote>
        <table class="layui-table" id="list" lay-filter="list"></table>
    </div>
</div>


<script type="text/html" id="action">
    <a href="{:url('edit')}?id={{d.id}}" class="layui-btn layui-btn-xs" lay-event="edit">{:lang('edit')}</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">{:lang('del')}</a>
</script>

<script type="text/html" id="status">
    <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="开启|关闭" lay-filter="status" {{ d.status == 1 ? 'checked' : '' }}>
</script>

<script type="text/html" id="link">
    <a target="_blank" href="{{d.url}}">{{d.url}}</a>
</script>
<script type="text/html" id="time">
    {{layui.util.toDateString(d.create_time*1000, 'yyyy-MM-dd HH:mm:ss')}}
</script>

{include file="common/footer"/}
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        var tableIn = table.render({
            elem: '#list',
            url: '{:url("index")}',
            method: 'post',
            cols: [[
                {checkbox: true, fixed: true},
                {field: 'id', title: 'ID', width: 80, fixed: true, sort: true},
                {field: 'name', title: '名称', width: 150, fixed: true,},
                {field: 'url', title: '链接', width: 180, templet:'#link'},
                {field: 'email', title: '邮箱', width: 180, templet:'#image'},
                {field: 'status', title: '状态', width: 180, templet:'#status'},
                {field: 'create_time', title: '添加时间', width: 180,templet:'#time'},
                // {field: 'update_time', title: '修改时间', width: 180,templet:'#time'},
                {title:'操作',width:150, toolbar: '#action',align:"center"} ,

            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true

        });



        table.on('tool(list)', function(obj){
            var data = obj.data;

            if(obj.event === 'del'){
                layer.confirm('{:lang("Are you sure you want to delete it")}', function(index){
                    loading =layer.load(1, {shade: [0.1,'#fff']});
                    $.post("{:url('delete')}",{id:data.id},function(res){
                        layer.close(loading);
                        layer.close(index);
                        if(res.code>0){
                            layer.msg(res.msg,{time:1000,icon:1});
                            obj.del();
                        }else{
                            layer.msg(res.msg,{time:1000,icon:2});
                        }
                    });
                });
            }


        });

        form.on('switch(status)', function(data){
                loading =layer.load(1, {shade: [0.1,'#fff']});
                $.post("{:url('state')}",{id:data.value},function(res){
                    layer.close(loading);
                    if(res.code>0){
                        layer.msg(res.msg,{time:1000,icon:1});
                    }else{
                        layer.msg(res.msg,{time:1000,icon:2});
                    }
                });
        });

        $('#search').click(function () {
            var $keys = $('#keys').val();
            if(!$keys){
                return layer.msg('请输入关键词');
            }

            tableIn.reload({ page: {page: 1},where: {keys: $keys}});

        })


    });


</script>